/*
 * @Author: HX
 * @Date: 2024-12-11 10:56:00
 * @LastEditors: HX
 * @LastEditTime: 2024-12-11 16:54:07
 */
let modelPostProcess = {
  CircledList:[],
  myFunction:null,
  add() {
    const shaderCode = this.getshaderCode();
      const material = new SSmap.Material();
      var fields = new SSmap.StringVector();
      material.shaderProgram =
          SSmap.ShaderProgram.createViewportQuadShader(
              shaderCode,
              fields
          );
      fields.delete();

      const postProcess = new SSmap.PostProcess();
      postProcess.material = material;
      window.viewer.renderSystem.addPostProcess(postProcess);
      const maxRadius = 20.0; //最大半径
      const duration = 0.1; //控制半径  扩散  持续时间

      var speed = 0.0;
      window.frameAction = window.viewer.addEventListener(
          "update",
          this.myFunction = function (dt) {
              const viewMatrix =
                  window.viewer.scene.mainCamera.viewMatrix();
              const center = SSmap.Matrix4.multiplyByVector3(
                  viewMatrix,
                  SSmap.Cartesian3.fromDegrees(
                      119.173652,
                      29.362142,
                      111.0
                  ).toVector3()
              );

              //let radius = (timer / duration) * maxRadius;
              material.setParameterVec3("centerEC", center);
              material.setParameterFloat("u_maxRadius", maxRadius);
              material.setParameterFloat("u_alpha", 10.5); //透明度
              material.setParameterFloat("u_cycle", 6.0); //周期
              material.setParameterFloat("u_count", 3.0);
              material.setParameterColor(
                  "u_scanColor",
                  SSmap.Color.fromRgb(26, 172, 255, 1)
              );
          }
      );
      window.material = material;
      this.CircledList.push(postProcess);
      //window.CircledList.push(material);
  },
  getshaderCode() {
    let constantval = window.isDeskTop ? '' : `#version 300 es
    precision highp float;
    precision highp sampler2D;`; //常数定义 客户端不需要应用
    return (
        constantval +
        `
                layout(location = 0) out vec4 FragColor;

                in vec2 v_textureCoordinates;

                uniform sampler2D colorTexture;       // 内置颜色缓存
                uniform sampler2D depthTexture;       // 内置深度缓存
                uniform mat4 inverseViewMatrix;       // 内置变量(ViewSpace->WorldSpace)
                uniform vec4 nearPlanes;              // 内置变量(近面视口坐标L,B,R,T)
                
                uniform vec4 u_scanColor;           //扫描颜色
                uniform vec3 centerEC;              //中心
                uniform float u_maxRadius;          //扫描最大半径
                uniform float u_alpha;              //透明度
                uniform float u_cycle;
                uniform float u_count;
                uniform float czm_frameNumber;

                vec3 computePositionEC(vec2 uv, float depth)
                {
                    vec3 pos = vec3(mix(nearPlanes.xy, nearPlanes.zw, uv), -1.0);
                    return pos * depth;
                }

                vec4 circle(vec4 color, float dist, float countradius)
                {
                    float radius = fract((czm_frameNumber - (countradius*u_cycle)) / (u_cycle*u_maxRadius)) * u_maxRadius;
                    if(dist < radius)
                    {
                        float factor =  dist / radius;
                        factor = pow(factor, u_alpha);
                        color = mix(color, u_scanColor, factor);
                    }
                   return color;
                }                
                
                

                void main()
                {
                    vec2 uv = v_textureCoordinates;

                    float depth = texture(depthTexture, uv).x;
                    vec3 positionEC = computePositionEC(uv, depth);
                    
                    float dist = length(positionEC - centerEC); 
                    vec4 color = texture(colorTexture, uv);

                    float radius =.0;
                    float countradius = u_maxRadius/u_count;
                    for(int i=0;i< int(u_count);i++){
                       color = circle(color,dist,countradius*float(i+1));                           
                    } 
                     FragColor = color; 
                }
            `
    );
  },
  remove() {
      if(window.isDeskTop){
          window.viewer.removeEventListener('update',this.myFunction)
      }
     if (window.frameAction) {
        window.frameAction.delete();
        window.frameAction = null;
        window.material.delete();
        window.material = null;
     }
     if (this.CircledList.length > 0) {
         this.CircledList.forEach((opt) => {
            opt.delete();
            opt = null
        });
     }
      this.CircledList = [];
  },
}

export { modelPostProcess }